//
//  RCollectTableViewCell.m
//  iPlan
//
//  Created by 侯伟 on 2021/9/4.
//

#import "RCollectTableViewCell.h"
#import "RVideoObj.h"
#import <SDWebImage/UIImageView+WebCache.h>

@interface RCollectTableViewCell (){
    UIImageView* _coverImage;
    UILabel* _infoLb;
    UILabel* _nameLb;
    UILabel* _performerLb;
    UILabel* _areaLb;
    UILabel* _yearLb;
    UILabel* _tagLb;
    UIButton* _playBtn;
    UIButton* _selectBtn;
}
/// 显示内容视图
@property(nonatomic,weak) MyLinearLayout* contentLayout;

@end

@implementation RCollectTableViewCell


- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if(self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]){
        
        self.backgroundColor = UIColor.whiteColor;
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        
        [self initSubViews];
        
        self.editable = NO;
    }
    return self;
}


- (void)initSubViews{
    
    MyLinearLayout *contentLayout = [MyLinearLayout linearLayoutWithOrientation:(MyOrientation_Horz)];
    contentLayout.padding = UIEdgeInsetsMake(8, 16, 8, 16);
    contentLayout.myMargin = 0;
    contentLayout.myHeight = MyLayoutSize.wrap;
    contentLayout.subviewHSpace = 10.0;
    [self.contentView addSubview:contentLayout];
    _contentLayout = contentLayout;
    
    UIButton* selectBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
    [selectBtn setImage:[UIImage imageNamed:@"common_select_n"] forState:UIControlStateNormal];
    [selectBtn setImage:[UIImage imageNamed:@"common_select_h"] forState:UIControlStateSelected];
    selectBtn.mySize = CGSizeMake(PX(28), PX(28));
    selectBtn.myCenterY = 0;
    [selectBtn addTarget:self action:@selector(selectClick:) forControlEvents:UIControlEventTouchUpInside];
    [contentLayout addSubview:selectBtn];
    _selectBtn = selectBtn;
        
    MyRelativeLayout* converLayout = [MyRelativeLayout new];
    converLayout.backgroundColor = COLOR_BACKGEOUND;
    converLayout.myHeight = PX(157);
    converLayout.myWidth = PX(105);
    converLayout.myMargin = 0;
    converLayout.layer.cornerRadius = PX(8);
    converLayout.layer.masksToBounds = YES;
    [contentLayout addSubview:converLayout];
    
    UIImageView* coverImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"common_placeholder"]];
    coverImage.contentMode = UIViewContentModeScaleAspectFill;
    coverImage.myMargin = 0;
    [converLayout addSubview:coverImage];
    _coverImage = coverImage;
    
    UILabel* infoLb = [UILabel lableWithFont:FONT_FIT(12) color:COLOR_WHITE text:@""];
    infoLb.backgroundColor = HEXACOLOR(0xFFFFFF5F);
    infoLb.textAlignment = NSTextAlignmentRight;
    infoLb.bottomPos.equalTo(coverImage.bottomPos);
    infoLb.myHorzMargin = 0;
    infoLb.myHeight = PX(20);
    [converLayout addSubview:infoLb];
    _infoLb = infoLb;
    
    MyRelativeLayout *infoLayout = [MyRelativeLayout new];
    infoLayout.padding = UIEdgeInsetsMake(8.0, 0, 8.0, 0);
    infoLayout.myMargin = 0;
    infoLayout.weight = 1.0;
    [contentLayout addSubview:infoLayout];
    
    UILabel* nameLb = [UILabel lableWithFont:FONT_FIT(15) color:COLOR_ORANGE text:@"影片名称"];
    nameLb.myHorzMargin = 0;
    nameLb.myHeight = MyLayoutSize.wrap;
    [infoLayout addSubview:nameLb];
    _nameLb = nameLb;
    
    UILabel* performerLb = [UILabel lableWithFont:FONT_FIT(12) color:COLOR_GRAY_BASE text:@"演员表"];
    performerLb.topPos.equalTo(nameLb.bottomPos).offset(8.0);
    performerLb.numberOfLines = 2;
    performerLb.myHeight = MyLayoutSize.wrap;
    performerLb.myHorzMargin = 0;
    [infoLayout addSubview:performerLb];
    _performerLb = performerLb;
    
    UILabel* areaLb = [UILabel lableWithFont:FONT_FIT(12) color:COLOR_GRAY_BASE text:@"地区"];
    areaLb.topPos.equalTo(performerLb.bottomPos).offset(8.0);
    areaLb.myHorzMargin = 0;
    [infoLayout addSubview:areaLb];
    _areaLb = areaLb;
    
    UILabel* yearLb = [UILabel lableWithFont:FONT_FIT(12) color:COLOR_GRAY_BASE text:@"年份"];
    yearLb.topPos.equalTo(areaLb.bottomPos).offset(8.0);
    yearLb.myHorzMargin = 0;
    [infoLayout addSubview:yearLb];
    _yearLb = yearLb;
        
    UIButton* playBtn = [UIButton buttonWithType:(UIButtonTypeCustom)];
    playBtn.userInteractionEnabled = NO;
    playBtn.backgroundColor = COLOR_THEME;
    [playBtn setTitle:@"立即播放" forState:UIControlStateNormal];
    [playBtn setTitleColor:COLOR_WHITE forState:UIControlStateNormal];
    playBtn.titleLabel.font = FONT_FIT(12);
    playBtn.myLeft = 0;
    playBtn.myBottom = 0;
    playBtn.mySize = CGSizeMake(PX(70), PX(24));
    playBtn.layer.cornerRadius = PX(12);
    [infoLayout addSubview:playBtn];
    _playBtn = playBtn;
}

- (void)setEditable:(BOOL)editable{
    _editable = editable;
    
    _selectBtn.visibility = editable?MyVisibility_Visible:MyVisibility_Gone;
    [_selectBtn sizeToFit];
}

- (void)setMovie:(RVideoObj *)movie{
    _movie = movie;
    
    //封面
    [_coverImage sd_setImageWithURL:[NSURL URLWithString:movie.img] placeholderImage:[UIImage imageNamed:@"common_placeholder"] completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
        if(error){
            [[SDWebImageManager sharedManager] removeFailedURL:imageURL];
        }
    }];
    
    //
    _infoLb.text = movie.info;
    [_infoLb sizeToFit];
    
    //影片名称
    _nameLb.text = movie.name;
    [_nameLb sizeToFit];
    
    //演员
    _performerLb.text = movie.actors;
    [_performerLb sizeToFit];
    
    //年代
    _yearLb.text = movie.year;
    [_yearLb sizeToFit];
    
    //地区
    _areaLb.text = movie.channel3_id;
    [_areaLb sizeToFit];
  
    _selectBtn.selected = movie.isSelected;
}

- (void)selectClick:(UIButton*)sender{
    sender.selected = !sender.selected;
    
    self.movie.isSelected = sender.selected;
    
    if(self.selectBlock){
        self.selectBlock(self.movie.isSelected);
    }
}
@end
